package com.yanqu.road.server.cmd.activity.snowgarden;

import com.yanqu.road.entity.activity.snowgarden.SnowGardenUserData;
import com.yanqu.road.entity.activity.snowgarden.config.SnowGardenConfig;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SnowGardenProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.snowgarden.SnowGardenModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.snowgarden.SnowGardenMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.HashMap;
import java.util.List;

@Cmd(code = GameProtocol.S_SNOW_GARDEN_GET_VISIT_RECORD, desc = "请求冰雪游园会拜访数据")
public class SnowGardenGetVisitRecordCmd extends AbstractCommand {
    private int page;        // 页数
    private int pageSize;    // 每页数据量
    private int recordType;  // 拜访类型

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SnowGardenProto.SnowGardenVisitRecordListReqMsg reqMsg = SnowGardenProto.SnowGardenVisitRecordListReqMsg.parseFrom(packet.getBytes());
        page = reqMsg.getPage();
        pageSize = reqMsg.getPageSize();
        recordType = reqMsg.getRecordType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SnowGardenConfig config = SnowGardenMgr.getSnowGardenConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_HAD_END;
            return;
        }
        SnowGardenProto.SnowGardenVisitRecordListReqMsg reqMsg = SnowGardenProto.SnowGardenVisitRecordListReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.getPage() > 1){
            SnowGardenProto.SnowGardenVisitRecordListRespMsg.Builder respMsg = SnowGardenProto.SnowGardenVisitRecordListRespMsg.newBuilder();
            respMsg.setRet(0);
            respMsg.setPage(reqMsg.getPage());
            respMsg.setPageSize(reqMsg.getPageSize());
            player.sendPacket(ClientProtocol.U_SNOW_GARDEN_GET_VISITOR_RECORD_LIST, respMsg);
            return;
        }

        ret = player.getModule(SnowGardenModule.class).getVisitRecordListMsg(recordType,false,new HashMap<Long, UserBaseInfo>());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            SnowGardenProto.SnowGardenVisitListRespMsg.Builder builder = SnowGardenProto.SnowGardenVisitListRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_SNOW_GARDEN_GET_VISITOR_RECORD_LIST, builder);
        }
    }
}
